class_name CardTooltip
extends Control

# UI组件引用
@onready var name_label: Label = $TooltipPanel/VBoxContainer/HeaderContainer/NameLabel
@onready var cost_label: Label = $TooltipPanel/VBoxContainer/HeaderContainer/CostLabel
@onready var type_label: Label = $TooltipPanel/VBoxContainer/TypeLabel
@onready var attack_label: Label = $TooltipPanel/VBoxContainer/StatsContainer/AttackContainer/AttackLabel
@onready var health_label: Label = $TooltipPanel/VBoxContainer/StatsContainer/HealthContainer/HealthLabel
@onready var description_label: RichTextLabel = $TooltipPanel/VBoxContainer/DescriptionLabel
@onready var rarity_label: Label = $TooltipPanel/VBoxContainer/RarityLabel
@onready var stats_container: HBoxContainer = $TooltipPanel/VBoxContainer/StatsContainer

# 提示框状态
var is_visible: bool = false
var target_card: Card = null

func _ready():
	"""初始化提示框"""
	# 默认隐藏
	visible = false
	modulate.a = 0.0
	
	# 设置鼠标过滤器，防止干扰其他UI
	mouse_filter = Control.MOUSE_FILTER_IGNORE

func show_tooltip(card: Control, mouse_position: Vector2):
	"""显示卡牌提示框"""
	if not card:
		return
	
	target_card = card
	update_tooltip_content(card)
	position_tooltip(mouse_position)
	
	# 显示动画
	visible = true
	is_visible = true
	
	# 使用Tween创建淡入效果
	var tween = create_tween()
	tween.tween_property(self, "modulate:a", 1.0, 0.2)

func hide_tooltip():
	"""隐藏卡牌提示框"""
	if not is_visible:
		return
	
	is_visible = false
	target_card = null
	
	# 使用Tween创建淡出效果
	var tween = create_tween()
	tween.tween_property(self, "modulate:a", 0.0, 0.15)
	tween.tween_callback(func(): visible = false)

func update_tooltip_content(card: Control):
	"""更新提示框内容"""
	if not card:
		return
	
	# 更新基本信息
	if name_label:
		name_label.text = card.card_name
	
	if cost_label:
		cost_label.text = str(card.cost)
	
	if type_label:
		var type_text = ""
		match card.card_type:
			"unit":
				type_text = "单位卡"
			"spell":
				type_text = "法术卡"
			_:
				type_text = card.card_type
		type_label.text = type_text
	
	# 更新属性（仅对单位卡显示）
	if card.card_type == "unit":
		if stats_container:
			stats_container.visible = true
		if attack_label:
			attack_label.text = str(card.attack)
		if health_label:
			health_label.text = str(card.health) + "/" + str(card.max_health)
	else:
		if stats_container:
			stats_container.visible = false
	
	# 更新描述
	if description_label:
		var desc_text = card.description
		if card.effect and card.effect != "":
			desc_text += "\n[color=yellow]" + get_effect_description(card.effect, card.effect_value) + "[/color]"
		description_label.text = desc_text
	
	# 更新稀有度
	if rarity_label:
		var rarity_text = ""
		var rarity_color = Color.WHITE
		match card.rarity:
			"common":
				rarity_text = "普通"
				rarity_color = Color.GRAY
			"rare":
				rarity_text = "稀有"
				rarity_color = Color.BLUE
			"epic":
				rarity_text = "史诗"
				rarity_color = Color.PURPLE
			"legendary":
				rarity_text = "传说"
				rarity_color = Color.ORANGE
			_:
				rarity_text = card.rarity
		
		rarity_label.text = rarity_text
		rarity_label.modulate = rarity_color

func get_effect_description(effect: String, value: int) -> String:
	"""获取效果描述文本"""
	match effect:
		"deal_damage":
			return "造成 " + str(value) + " 点伤害"
		"heal":
			return "恢复 " + str(value) + " 点生命值"
		"draw_card":
			return "抽 " + str(value) + " 张牌"
		"damage_and_heal":
			return "造成 " + str(value) + " 点伤害并恢复 " + str(value) + " 点生命值"
		"buff_health_shield":
			return "获得 " + str(value) + " 点护盾"
		"battlecry_draw":
			return "战吼：抽 " + str(value) + " 张牌"
		_:
			return effect

func position_tooltip(mouse_position: Vector2):
	"""定位提示框位置"""
	if not get_viewport():
		return
	
	var viewport_size = get_viewport().get_visible_rect().size
	var tooltip_size = size
	
	# 默认在鼠标右侧显示
	var target_position = mouse_position + Vector2(20, -tooltip_size.y / 2)
	
	# 检查是否超出屏幕边界
	if target_position.x + tooltip_size.x > viewport_size.x:
		# 如果右侧超出，显示在鼠标左侧
		target_position.x = mouse_position.x - tooltip_size.x - 20
	
	if target_position.y < 0:
		# 如果上方超出，调整到屏幕内
		target_position.y = 10
	elif target_position.y + tooltip_size.y > viewport_size.y:
		# 如果下方超出，调整到屏幕内
		target_position.y = viewport_size.y - tooltip_size.y - 10
	
	position = target_position

func update_position(mouse_position: Vector2):
	"""更新提示框位置（跟随鼠标）"""
	if is_visible:
		position_tooltip(mouse_position)
